package per.lance.easytomcat.main;

import per.lance.easytomcat.dispather.DispatcherServlet;
import per.lance.easytomcat.net.HttpRequest;
import per.lance.easytomcat.net.HttpRespone;
import per.lance.easytomcat.net.HttpSocket;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URISyntaxException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
    public static void main(String[] args) throws URISyntaxException {
        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            DispatcherServlet dispatcherServlet = new DispatcherServlet();
            ExecutorService executorService = Executors.newFixedThreadPool(30);
            boolean key = true;
            while(key) {
                Socket socket = serverSocket.accept();
                HttpSocket clientSocket  = new HttpSocket(socket);
                HttpRequest httpRequest = clientSocket.getHttpRequest();
                HttpRespone httpRespone = clientSocket.getHttpRespone();
                executorService.execute(() -> dispatcherServlet.doDispatch(httpRequest, httpRespone));
            }
            executorService.shutdown();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
